<?php

/**
 * This file contains Kernel class and Exception
 * 
 * @package DeadTouch AAC
 * @version 0.0.1
 * @author Kofel <kofels@gmail.com>
 * @copyright 2008 (C) by Kofel
 * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public License, Version 3
 * @todo future: :D
 */

/**
 * Kernel Exception
 * 
 * @package DeadTouch AAC
 * @version 0.0.1
 */
 
class KernelException extends Exception
{
	//Kernel Exception
}


/**
 * Kernel Class
 * 
 * @package DeadTouch AAC
 * @version 0.0.1
 */

class Kernel
{
	/**
	  * Layers list
 	  * 
 	  * <p>
 	  * List of an layers
 	  * </p>
 	  * 
 	  * @var array
 	  */
 	  
 	 private static $Layers = array();
 	 
 	 private static $dLayers = array();

 	  /**
	   * Return a layer instance
	   * 
	   * <p>
		   * Use this method if you want to get a layer instance
	   * </p>
	   * 
	   * @param string $name layer name
	   * @example database
	   */
 	 
 	public static function layer( $name )
	{
	 	if( self::$Layers[ $name ] )
		{
			return self::$Layers[ $name ];
		}
		
		return false;
	}

 	  /**
	   * Load new layer
	   * 
	   * <p>
		   * Use this method if you want to load new layer. Layer you can call by Kernel::layer() method.
	   * </p>
	   * 
	   * @param string $name layer name
	   * @example database
	   */
 	 
 	public static function loadLayer( $name )
	{
	 	$_t = $name . 'Layer';
	 	require_once 'system/layer.' . $name . '.php';
	 	if( file_exists( 'system/layer.' . $name . '.php' ) and class_exists( $_t ) )
		{
		 	self::$Layers[ $name ] = new $_t;
		 	return true;
		}
		else
		{
			throw new KernelException('<strong>' . $name . '</strong> layer does not exists.');
		}
	}
	 
	 /**
	   * Load all layers
	   * 
	   * <p>
	   * Call this method if you want runs in every layer load() method
	   * </p>
	   */
	   
	public static function loadLayers()
	{
		foreach( self::$Layers as $Name => $Layer )
		{
			if( method_exists( $Layer, 'load' ) and !self::$dLayers[ $Name ] )
			{
				$Layer->load();
			}
		}
		
		return true;
	}
	
	 /**
	   * Unload all layers
	   * 
	   * <p>
	   * Call this method if you want runs in every layer unload() method
	   * </p>
	   */
	
	public static function unloadLayers()
	{
		foreach( self::$Layers as $Layer )
		{
			if( method_exists( $Layer, 'unload' ) )
			{
				$Layer->unload();
			}
		}
		
		return true;
	}
	
	public static function dropLayer( $name )
	{
		unset( self::$Layers[ $name ] );
		self::$dLayers[ $name ] = true;
		return true;
	}
}

/**
 * End of kernel Class
 */

?>